package com.lianzhihui.romeex.widget.dialog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.dataprovider.LineDataProvider;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.OrePoolBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.ui.base.BaseDialog;
import com.lianzhihui.romeex.utils.DateUtil;
import com.lianzhihui.romeex.utils.NumberUtils;
import com.lianzhihui.romeex.utils.PreferenceUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.widget.dialog
 * @ClassName: OrePoolChartDialog
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/24 9:55
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/24 9:55
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class OrePoolChartDialog extends BaseDialog {
    @BindView(R.id.chart1)
    LineChart chart1;
    @BindView(R.id.select_one)
    CardView selectOne;
    @BindView(R.id.renaissance)
    TextView renaissance;
    @BindView(R.id.tv_price)
    TextView tv_price;
    @BindView(R.id.tv_team_fx)
    TextView tv_team_fx;
    @BindView(R.id.layout_one)
    RelativeLayout layoutOne;
    @BindView(R.id.select_two)
    CardView selectTwo;
    @BindView(R.id.trend)
    TextView trend;
    @BindView(R.id.layout_two)
    RelativeLayout layoutTwo;
    private List<OrePoolBean.PledgeAmountTrendBean.SevenPledgeAmountBean> sevenPledgeAmount;
    private List<OrePoolBean.PriceTrendBean.SevenPriceBean> sevenPrice;
    private String wholePledgeAmount;

    public OrePoolChartDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int provideViewId() {
        return R.layout.dialog_chart;
    }

    @Override
    protected void initView() {
        initChart1();
        tv_team_fx.setText(MessageFormat.format("{0}R", sevenPledgeAmount.get(sevenPledgeAmount.size() - 1).getPledgeAmount()));
    }

    private void setTitlePrice() {
        String stringPreference = PreferenceUtils.getString(AppConstants.CURRENCY_TYPE_KEY, "");
//        if (stringPreference.equals(AppConstants.CNY)) {
//            tv_price.setText(MessageFormat.format("{0}{1}", sevenPrice.get(sevenPrice.size() - 1).getCoinPriceCny(), context.getString(R.string.string_cny)));
//        } else if (stringPreference.equals(AppConstants.USD)) {
        tv_price.setText(MessageFormat.format("{0}{1}", NumberUtils.big8(Double.parseDouble(sevenPrice.get(sevenPrice.size() - 1).getCoinPrice())), context.getString(R.string.text_usdt)));
//        } else {
//            tv_price.setText(MessageFormat.format("{0}{1}", sevenPrice.get(sevenPrice.size() - 1).getCoinPriceCny(), context.getString(R.string.string_cny)));
//        }
    }

    @OnClick({R.id.layout_one, R.id.layout_two})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_one:
                selectOne.setVisibility(View.VISIBLE);
                selectTwo.setVisibility(View.INVISIBLE);
                initChart1();
                break;
            case R.id.layout_two:
                selectOne.setVisibility(View.INVISIBLE);
                selectTwo.setVisibility(View.VISIBLE);
                initChart2();
                break;
            default:
                break;
        }
    }


    public void setData(List<OrePoolBean.PledgeAmountTrendBean.SevenPledgeAmountBean> sevenPledgeAmount, List<OrePoolBean.PriceTrendBean.SevenPriceBean> sevenPrice, String wholePledgeAmount) {
        this.sevenPledgeAmount = sevenPledgeAmount;
        Collections.sort(sevenPledgeAmount);
        this.sevenPrice = sevenPrice;
        Collections.sort(sevenPrice);
        this.wholePledgeAmount = wholePledgeAmount;
    }


    private void initChart1() {
        chart1.setViewPortOffsets(150, 50, 60, 100);
//        chart1.setBackgroundColor(Color.rgb(104, 0, 0));

        // no description text
        chart1.getDescription().setEnabled(false);

        // 触摸
        chart1.setTouchEnabled(false);

        //拖拽
        chart1.setDragEnabled(false);
        //缩放
        chart1.setScaleEnabled(false);

        // if disabled, scaling can be done on x- and y-axis separately
        chart1.setPinchZoom(false);

        chart1.setDrawGridBackground(false);
//        chart1.setMaxHighlightDistance(300);
//        chart1.setExtraBottomOffset(50);

        YAxis y = chart1.getAxisLeft();
        y.setLabelCount(6, false);
        y.setDrawAxisLine(false);
        y.setTextColor(context.getResources().getColor(R.color.text_999999_color));
        y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        y.setGridColor(context.getResources().getColor(R.color.text_999999_color));
        y.setAxisLineColor(context.getResources().getColor(R.color.chart_xais_line_color));
        y.setAxisMinimum(0f);
        chart1.getAxisRight().setEnabled(false);
        XAxis xAxis = chart1.getXAxis();
        xAxis.setLabelCount(6, false);
        //是否可用
        xAxis.setEnabled(true);
        //是否显示数值
        xAxis.setDrawLabels(true);
        //是否显示坐标线
        xAxis.setDrawAxisLine(true);
        //设置坐标轴线的颜色
        xAxis.setAxisLineColor(context.getResources().getColor(R.color.chart_xais_line_color));
        //设置坐标轴线的宽度
        xAxis.setAxisLineWidth(0.8f);
        //是否显示竖直风格线
        xAxis.setDrawGridLines(false);
        //X轴文字颜色
        xAxis.setTextColor(context.getResources().getColor(R.color.text_999999_color));
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//X轴文字大小
//        xAxis.setSpaceMin(20f);//左空白区大小
//        xAxis.setSpaceMax(20f);//右空白区大小

        // lower max, as cubic runs significantly slower than linear

        chart1.getLegend().setEnabled(false);

        chart1.animateXY(1000, 1000);
        setData1();
        // don't forget to refresh the drawing
        chart1.requestLayout();
        chart1.notifyDataSetChanged();
        chart1.invalidate();
    }

    private void setData1() {

        ArrayList<Entry> values = new ArrayList<>();

        setTitlePrice();
        for (int i = 0; i < sevenPledgeAmount.size(); i++) {
            values.add(new Entry(i, Float.parseFloat(sevenPledgeAmount.get(i).getPledgeAmount()), sevenPledgeAmount.get(i)));
        }
//
//        values.add(new Entry(2, Float.parseFloat(sevenPledgeAmount.get(1).getPledgeAmount()), sevenPledgeAmount.get(1)));
//        values.add(new Entry(3, Float.parseFloat(sevenPledgeAmount.get(2).getPledgeAmount()), sevenPledgeAmount.get(2)));
//        values.add(new Entry(4, Float.parseFloat(sevenPledgeAmount.get(3).getPledgeAmount()), sevenPledgeAmount.get(3)));
//        values.add(new Entry(5, Float.parseFloat(sevenPledgeAmount.get(4).getPledgeAmount()), sevenPledgeAmount.get(4)));
//        values.add(new Entry(6, Float.parseFloat(sevenPledgeAmount.get(5).getPledgeAmount()), sevenPledgeAmount.get(5)));
//        values.add(new Entry(7, Float.parseFloat(sevenPledgeAmount.get(6).getPledgeAmount()), sevenPledgeAmount.get(6)));
        final ArrayList<String> xLabel = new ArrayList<>();
        for (OrePoolBean.PledgeAmountTrendBean.SevenPledgeAmountBean item : sevenPledgeAmount) {
            xLabel.add(DateUtil.sec2Date(item.getTimeStamp(), "dd/MM"));
        }
        XAxis xAxis = chart1.getXAxis();
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return xLabel.get((int) value);
            }
        });
        LineDataSet set1;

        if (chart1.getData() != null &&
                chart1.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) chart1.getData().getDataSetByIndex(0);
            set1.setValues(values);
            setChartOneColor(set1, R.color.chart_color_one, R.drawable.fade_cyan);
            chart1.getData().notifyDataChanged();

            chart1.notifyDataSetChanged();
        } else {
            // create a dataset and give it a type
            set1 = new LineDataSet(values, "DataSet 1");
            setChartOneColor(set1, R.color.chart_color_one, R.drawable.fade_cyan);
//            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
//            set1.setCubicIntensity(0.2f);
//            set1.setDrawFilled(true);
//            set1.setDrawCircles(false);
//            set1.setLineWidth(0.5f);
//            set1.setCircleRadius(4f);
//            set1.setCircleColor(Color.RED);
////            set1.setHighLightColor(Color.rgb(244, 117, 117));
//            set1.setColor(context.getResources().getColor(R.color.chart_color_one));
//            Drawable drawable = context.getResources().getDrawable(R.drawable.fade_cyan);
//            set1.setFillDrawable(drawable);
//            set1.setDrawHorizontalHighlightIndicator(false);
//            set1.setFillFormatter(new IFillFormatter() {
//                @Override
//                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
//                    return chart1.getAxisLeft().getAxisMinimum();
//                }
//            });

            // create a data object with the data sets
            LineData data = new LineData(set1);
//            data.setValueTextSize(9f);
            data.setDrawValues(false);
            // set data
            chart1.setData(data);
        }
    }

    private void setChartOneColor(LineDataSet set1, int color, int drawableId) {
        set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        set1.setCubicIntensity(0.2f);
        set1.setDrawFilled(true);
        set1.setDrawCircles(false);
        set1.setLineWidth(0.5f);
        set1.setCircleRadius(4f);
        set1.setCircleColor(Color.RED);
//            set1.setHighLightColor(Color.rgb(244, 117, 117));
        set1.setColor(context.getResources().getColor(color));
        Drawable drawable = context.getResources().getDrawable(drawableId);
        set1.setFillDrawable(drawable);
        set1.setDrawHorizontalHighlightIndicator(false);
        set1.setFillFormatter(new IFillFormatter() {
            @Override
            public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
                return chart1.getAxisLeft().getAxisMinimum();
            }
        });

    }

    private void initChart2() {
        chart1.setViewPortOffsets(150, 50, 60, 100);
//        chart1.setBackgroundColor(Color.rgb(104, 0, 0));

        // no description text
        chart1.getDescription().setEnabled(false);

        // 触摸
        chart1.setTouchEnabled(false);

        //拖拽
        chart1.setDragEnabled(false);
        //缩放
        chart1.setScaleEnabled(false);

        // if disabled, scaling can be done on x- and y-axis separately
        chart1.setPinchZoom(false);

        chart1.setDrawGridBackground(false);
//        chart1.setMaxHighlightDistance(300);
//        chart1.setExtraBottomOffset(50);
        YAxis y = chart1.getAxisLeft();
        y.setLabelCount(6, false);
        y.setTextColor(context.getResources().getColor(R.color.text_999999_color));
        //是否显示坐标线
        y.setDrawAxisLine(false);
        y.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        y.setDrawGridLines(true);
        y.setGridColor(context.getResources().getColor(R.color.text_999999_color));
        y.setAxisMinimum(0f);
        y.setAxisLineColor(context.getResources().getColor(R.color.chart_xais_line_color));
//        y.setAxisLineColor(Color.BLACK);
        chart1.getAxisRight().setEnabled(false);
        XAxis xAxis = chart1.getXAxis();

        xAxis.setLabelCount(6, false);
        //是否可用
        xAxis.setEnabled(true);
        //是否显示数值
        xAxis.setDrawLabels(true);
        //是否显示坐标线
        xAxis.setDrawAxisLine(true);
        //设置坐标轴线的颜色
        xAxis.setAxisLineColor(context.getResources().getColor(R.color.chart_xais_line_color));
        //设置坐标轴线的宽度
        xAxis.setAxisLineWidth(0.8f);
        //是否显示竖直风格线
        xAxis.setDrawGridLines(false);
        //X轴文字颜色
        xAxis.setTextColor(context.getResources().getColor(R.color.text_999999_color));
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setTextSize(10f);//X轴文字大小
//        xAxis.setSpaceMin(20f);//左空白区大小
//        xAxis.setSpaceMax(20f);//右空白区大小
        // lower max, as cubic runs significantly slower than linear

        chart1.getLegend().setEnabled(false);

        chart1.animateXY(1000, 1000);
        setData2(6, 60);
        // don't forget to refresh the drawing
        chart1.requestLayout();
        chart1.notifyDataSetChanged();
        chart1.invalidate();
    }

    private void setData2(int count, float range) {

        ArrayList<Entry> values = new ArrayList<>();

        setTitlePrice();

        for (int i = 0; i < sevenPledgeAmount.size(); i++) {
            values.add(new Entry(i, Float.parseFloat(sevenPrice.get(i).getCoinPrice())));
        }

//        values.add(new Entry(2, Float.parseFloat(sevenPrice.get(1).getCoinPrice())));
//        values.add(new Entry(3, Float.parseFloat(sevenPrice.get(2).getCoinPrice())));
//        values.add(new Entry(4, Float.parseFloat(sevenPrice.get(3).getCoinPrice())));
//        values.add(new Entry(5, Float.parseFloat(sevenPrice.get(4).getCoinPrice())));
//        values.add(new Entry(6, Float.parseFloat(sevenPrice.get(5).getCoinPrice())));
//        values.add(new Entry(7, Float.parseFloat(sevenPrice.get(6).getCoinPrice())));
        final ArrayList<String> xLabel = new ArrayList<>();
        for (OrePoolBean.PriceTrendBean.SevenPriceBean item : sevenPrice) {
            xLabel.add(DateUtil.sec2Date(item.getTimestamp(), "dd/MM"));
        }
        XAxis xAxis = chart1.getXAxis();
        xAxis.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                return xLabel.get((int) value  );
            }
        });
        LineDataSet set1;
        if (chart1.getData() != null &&
                chart1.getData().getDataSetCount() > 0) {
            set1 = (LineDataSet) chart1.getData().getDataSetByIndex(0);
            set1.setValues(values);
            setChartOneColor(set1, R.color.chart_color_two, R.drawable.fade_orange);
            chart1.getData().notifyDataChanged();
            chart1.notifyDataSetChanged();
        }
//        else {
        // create a dataset and give it a type
//            set1 = new LineDataSet(values, "DataSet 1");
//
//            set1.setMode(LineDataSet.Mode.CUBIC_BEZIER);
//            set1.setCubicIntensity(0.2f);
//            set1.setDrawFilled(true);
//            set1.setDrawCircles(false);
//            set1.setLineWidth(0.5f);
//            set1.setCircleRadius(4f);
//            set1.setCircleColor(Color.RED);
////            set1.setHighLightColor(Color.rgb(244, 117, 117));
//            set1.setColor(context.getResources().getColor(R.color.chart_color_two));
//            Drawable drawable = context.getResources().getDrawable(R.drawable.fade_orange);
//            set1.setFillDrawable(drawable);
//            set1.setDrawHorizontalHighlightIndicator(false);
//            set1.setFillFormatter(new IFillFormatter() {
//                @Override
//                public float getFillLinePosition(ILineDataSet dataSet, LineDataProvider dataProvider) {
//                    return chart1.getAxisLeft().getAxisMinimum();
//                }
//            });
//
//            // create a data object with the data sets
//            LineData data = new LineData(set1);
////            data.setValueTextSize(9f);
//            data.setDrawValues(false);
//            // set data
//            chart1.setData(data);
//        }
    }

    @Override
    public void dismiss() {
        super.dismiss();

    }

}
